package com.shigui.news.controller.api;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.shigui.news.controller.BaseAction;
import com.shigui.news.enums.ResponseCode;
import com.shigui.news.enums.UserType;
import com.shigui.news.exception.UserException;
import com.shigui.news.interceptor.JsonFormat;
import com.shigui.news.model.User;
import com.shigui.news.service.UserService;
@Scope("prototype")
@Controller("userAction")
public class UserAction extends BaseAction {
	@Autowired
	private UserService userService;
	private User user;
	
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	
	
	public void login(){
		
		System.out.println("开始");
		/**
		 * 根据用户密码，用户名登录
		 */
		User userLogin = userService.login(user.getUsername(), user.getCipherPassword());
		
		Map<String, Object> session = ActionContext.getContext().getSession();
		/**
		 * 将用户信息保存到Session中
		 */
		
		session.put(User.SESSION_PERSIST_KEY, userLogin.getPersistCode());
		session.put(User.SESSION_USER_KEY, userLogin);
		/**
		 * 返回结果，Json格式形式
		 */
		getHttpRequest().setAttribute(JsonFormat.RESPONSE_DATE_KEY, userLogin);
	}
	
	
	public void logout(){
		/**
		 * 清除session
		 */
		userService.logout();
		
	}
	
	public void add(){
		
		
		userService.insert(user);
		
		getHttpRequest().setAttribute(JsonFormat.RESPONSE_DATE_KEY, user);
		
	}
	public void delete(){
		
		User loginUser = findByPersistCodeOrFail();
		
		if(loginUser.getType().equals(UserType.MANAGER.getCode())){
		
			User uuser = userService.selectById(user.getId());
			
			getHttpRequest().setAttribute(JsonFormat.RESPONSE_DATE_KEY,uuser );
			
			userService.delete(uuser);
		
		}else{
			throw new UserException(ResponseCode.USER_INVALID_OPERATION);
		}
		
	}
	
	
	public void register(){
		
		User loginUser = findByPersistCodeOrFail();
		
		if(loginUser == null){
			
			add();
		}
		
		
		
	}
	
	public void update(){
		
		User loginUser = findByPersistCodeOrFail();
		
		loginUser.setAddress(user.getAddress());
		
		loginUser.setAge(user.getAge());
		
		userService.update(loginUser);
	}
	
	/**
	 * 修改个人密码
	 */
	public void updatePassword(){
		//修改密码失败 100004
		User loginUser = findByPersistCodeOrFail();
		
		loginUser.setPassword(user.getPassword());
		
		
		userService.update(loginUser);
	}
	/**
	 * 查看个人信息
	 */
	
	public void selectById(){
		
		User loginUser = findByPersistCodeOrFail();
		
		
		
		getHttpRequest().setAttribute(JsonFormat.RESPONSE_MESSAGE_KEY, "查找成功");
		getHttpRequest().setAttribute(JsonFormat.RESPONSE_DATE_KEY, loginUser);
		
		
	}
	
	public void select(){
		
		User loginUser = findByPersistCodeOrFail();
//		User findUser = new User();
//		findUser.setId(user.getId());
		User userrUser = userService.selectById(user.getId());
		
		getHttpRequest().setAttribute(JsonFormat.RESPONSE_MESSAGE_KEY, "查找成功");
		getHttpRequest().setAttribute(JsonFormat.RESPONSE_DATE_KEY, userrUser);
		
	}
}
